package com.zz.hhbl.ui.qrcodescan

import android.content.Context.VIBRATOR_SERVICE
import android.os.Bundle
import android.os.Vibrator
import androidx.core.content.ContextCompat.getSystemService
import cn.bingoogolapple.qrcode.core.QRCodeView
import cn.bingoogolapple.qrcode.zxing.ZXingView
import com.zz.framework.core.base.BaseActivity
import com.zz.framework.core.ext.onClick
import com.zz.framework.core.ext.toLanguage
import com.zz.framework.core.ext.viewBinding
import com.zz.framework.core.toast.ToastUtils
import com.zz.framework.core.utils.LogUtils
import com.zz.framework.core.utils.language.Strings
import com.zz.hhbl.R
import com.zz.hhbl.config.GlobalCache
import com.zz.hhbl.databinding.ActivityScanLayoutBinding
import com.zz.hhbl.event.ScanResultEvent
import org.greenrobot.eventbus.EventBus

/**
 * Copyright © 2021 zanezhao All rights reserved.
 * @version: V1.0
 */
class ScanActivity : BaseActivity(), QRCodeView.Delegate {
    val binding: ActivityScanLayoutBinding by viewBinding()
    val prefix = "https://www.lhhwl.com/jqcx/lihuanhuan.html?jqid=${GlobalCache.getSpotInfo().id}&orderBy="
    override fun initView(savedInstanceState: Bundle?) {
        binding.zxingview.setDelegate(this)
        binding.btnBack.onClick { finish() }
    }


    override fun onResume() {
        super.onResume()
        binding.zxingview.startCamera() // 打开后置摄像头开始预览，但是并未开始识别, startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT); // 打开前置摄像头开始预览，但是并未开始识别
        binding.zxingview.startSpotAndShowRect() // 显示扫描框，并开始识别
        binding.zxingview.scanBoxView.tipText = Strings.将条码放入框内即可自动扫描.toLanguage()
    }

    override fun onPause() {
        binding.zxingview.stopCamera() // 关闭摄像头预览，并且隐藏扫描框
        super.onPause()
    }

    override fun onDestroy() {
        binding.zxingview.onDestroy() // 销毁二维码扫描控件
        super.onDestroy()
    }

    private fun vibrate() {
        val vibrator = getSystemService(VIBRATOR_SERVICE) as Vibrator
        vibrator.vibrate(200)
    }

    fun error(message: String) {
        binding.zxingview.startSpot()
    }

    override fun onScanQRCodeSuccess(result: String?) {
        vibrate()
        binding.zxingview.stopSpot()
        handleResult(result)
    }

    fun handleResult(result: String?) {
        LogUtils.error("result:$result")
        if (result?.startsWith(prefix) == true) {
            val orderBy = result.removePrefix(prefix)
            EventBus.getDefault().post(ScanResultEvent(orderBy))
            finish()
        } else {
            ToastUtils.show(Strings.此二维码非李欢欢AI畅游宝二维码.toLanguage())
            binding.zxingview.startSpot()
        }
    }


    override fun onCameraAmbientBrightnessChanged(isDark: Boolean) {
        // 这里是通过修改提示文案来展示环境是否过暗的状态，接入方也可以根据 isDark 的值来实现其他交互效果
//        var tipText = binding.zxingview.scanBoxView.tipText
//        val ambientBrightnessTip = getString(R.string.text_too_dark_to_turn_on_flash)
//        if (isDark) {
//            if (!tipText.contains(ambientBrightnessTip)) {
//                binding.zxingview.scanBoxView.tipText = tipText + ambientBrightnessTip
//            }
//        } else {
//            if (tipText.contains(ambientBrightnessTip)) {
//                tipText = tipText.substring(0, tipText.indexOf(ambientBrightnessTip))
//                binding.zxingview.scanBoxView.tipText = tipText
//            }
//        }
    }

    override fun onScanQRCodeOpenCameraError() {
        Strings.打开相机出错.toLanguage()
    }

}